#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace bit
{
	template<class T>
	class vector
	{
		typedef T* iterator;
		typedef const T* const_iterator;
	public:
		vector() = default;

		vector( vector<T>& v)
		{
			reserve(v.size());
			for (auto e : v)
			{
				push_back(e);
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);

		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);

			return *this;
		}

		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}
		size_t size()
		{
			return _finish - _start;
		}

		size_t capacity()
		{
			return _end_of_storage - _start;
		}

		void reserve(size_t n)
		{
			if (n>capacity())
			{
				size_t oldsize = size();
				T* tmp = new T[n];
				memcpy(tmp, _start, size() * sizeof(T));
				delete[]_start;

				_start = tmp;
				_finish = tmp + oldsize;
				_end_of_storage = tmp + n;
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
		}

		bool empty()
		{
			return _finish == _start;
		}
		void pop_back()
		{
			if (!empty())
			{
				--_finish;
			}
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity * 2);
				pos = _start + len;
			}
			iterator end = _finish-1;
			while (end >= pos)
			{
				*(end + 1) = *end;
			}
			*pos = x;
			++_finish;
		}

		void erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator it = pos + 1;
			while (it != end())
			{
				*(it - 1) = *it;
				++it;
			}
			--_finish;
		}
		T& operator[](size_t i)
		{
			assert(i < size());

			return _start[i];
		}
		const T& operator[](size_t i)const
		{
			return _start[i];
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};

	template <class T>
	void paint_vector(const vector<T>& v)
	{
		auto it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
		/*for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;*/
	}
	void test1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		paint_vector(v);
		
	}

	void test2()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		vector<int> v2=v1;
		paint_vector(v2);

	}

	void test3()
	{
		vector<int> v1;
		vector<int> v2;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v2 = v1;
		paint_vector(v2);
	}
}